package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.TCheckMapper;
import edu.gxnu.cetcdemo.pojo.TCheck;
import edu.gxnu.cetcdemo.service.TCheckService;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Uplifted
* @description 针对表【t_check】的数据库操作Service实现
* @createDate 2024-04-02 15:09:11
*/
@Service
public class TCheckServiceImpl extends ServiceImpl<TCheckMapper, TCheck>
    implements TCheckService{

    @Autowired
    private TCheckMapper tCheckMapper;

    @Override
    public Result queryTCheck() {
        Map data = new HashMap();
        List<TCheck> list = tCheckMapper.selectList(null);
        data.put("tCheckList", list);
        return Result.build(200, "成功查询所有信息", data);
    }

    @Override
    public Result addTCheck(TCheck tCheck) {
        // 是否存在相同信息的记录
        LambdaQueryWrapper<TCheck> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TCheck::getCheckName, tCheck.getCheckName())
                .eq(TCheck::getCheckPhone, tCheck.getCheckPhone())
                .eq(TCheck::getCheckTime, tCheck.getCheckTime())
                .eq(TCheck::getCheckWeek, tCheck.getCheckWeek());
        TCheck tTCheck = tCheckMapper.selectOne(lambdaQueryWrapper);

        if(tTCheck != null){
            return Result.build(501, "该信息已存在");
        }

        tCheckMapper.insert(tCheck);
        return Result.build(200, "成功添加信息");
    }

    @Override
    public Result updateTCheck(TCheck tCheck) {
        // 是否存在相同信息的记录
        LambdaQueryWrapper<TCheck> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TCheck::getCheckName, tCheck.getCheckName())
                .eq(TCheck::getCheckPhone, tCheck.getCheckPhone())
                .eq(TCheck::getCheckTime, tCheck.getCheckTime())
                .eq(TCheck::getCheckWeek, tCheck.getCheckWeek());
        TCheck tTCheck = tCheckMapper.selectOne(lambdaQueryWrapper);

        if(tTCheck != null){
            return Result.build(501, "存在相同信息");
        }

        tCheckMapper.update(tCheck, new LambdaQueryWrapper<TCheck>().eq(TCheck::getCheckId, tCheck.getCheckId()));
        return Result.build(200, "成功更新信息");
    }

    @Override
    public Result delTCheck(long id) {
        LambdaQueryWrapper<TCheck> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        tCheckMapper.delete(lambdaQueryWrapper.eq(TCheck::getCheckId, id));
        return Result.build(200, "成功删除信息");
    }
}




